<?php

class ImgSplice {
  /**
   * 扫描路径
   * @var string
   */
  var $path_dir = "";
  /**
   * 路径中图片数组
   * @var [type]
   */
  var $F_Array = null;
  /**
   * 静态文件类型
   * @var array
   */
  
  var $ThumbWidth = 600;

  var $ThumbHeight =0;

  var $ImagesTypes = array(
      1 => 'GIF', 
      2 => 'JPG', 
      3 => 'PNG', 
      4 => 'SWF', 
      5 => 'PSD', 
      6 => 'BMP', 
      7 => 'TIFF(intel byte order)', 
      8 => 'TIFF(motorola byte order)', 
      9 => 'JPC', 
      10 => 'JP2', 
      11 => 'JPX', 
      12 => 'JB2', 
      13 => 'SWC', 
      14 => 'IFF', 
      15 => 'WBMP', 
      16 => 'XBM'
  );
  
  


  function __construct($_path_dir = null) {
    if (is_null($_path_dir)) {
      logCmd("Class:ImgSplice:$_path_dir => NULL");
    }
    $this -> path_dir = $_path_dir;
  }

  private function GenerateImg(){
      if (function_exists('imagecreatetruecolor')) $GenerateImg = imagecreatetruecolor($this->ThumbWidth, $this->ThumbHeight);
      else  $GenerateImg = imagecreate($this->ThumbWidth, $this->ThumbHeight); 
      $GenerateHeight = 0;
      foreach ($this->F_Array as $key => $img) {
          $createFun = 'ImageCreateFrom' . ($img['type'] == 'JPG' ? 'jpeg' : strtolower($img['type']) );
          $srcImg = $createFun($img['path']);
          if (function_exists("ImageCopyResampled"))
          imagecopyresampled($GenerateImg, $srcImg, 0, $GenerateHeight, 0, 0, $img["width"], $img["height"], $img["width"] , $img["height"] );
          else
          imagecopyresized($GenerateImg, $srcImg, 0, $GenerateHeight, 0, 0, $img["width"], $img["height"], $img["width"] , $img["height"] );          

          $GenerateHeight+=($img["height"]+0);
      }
      imagejpeg($GenerateImg,$this->path_dir."/generateImg.jpg");
  }  




  /**
  *  缩略图片 内部方法
  * @param  [type] $img [description]
  * @return [type]      [description]
  */
  private function thumbImage($img){
      $scale= $this->ThumbWidth /$img["width"];

      $width = (int) ($img["width"] * $scale);
      $height = (int) ($img["height"] * $scale);
      $type = strtolower($img['type']);
      $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);

     
      $srcImg = $createFun($img['path']);
      if ($type != 'gif' && function_exists('imagecreatetruecolor')) $thumbImg = imagecreatetruecolor($width, $height);
      else  $thumbImg = imagecreate($width, $height);  
      if('png'==$type){
                imagealphablending($thumbImg, false);//取消默认的混色模式（为解决阴影为绿色的问题）
                imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息（为解决阴影为绿色的问题）    
      }elseif('gif'==$type){
          $trnprt_indx = imagecolortransparent($srcImg);
           if ($trnprt_indx >= 0) {
                  //its transparent
                 $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
                 $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                 imagefill($thumbImg, 0, 0, $trnprt_indx);
                 imagecolortransparent($thumbImg, $trnprt_indx);
        }
      }
      // 复制图片
      if (function_exists("ImageCopyResampled"))
          imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $img["width"] , $img["height"] );
      else
          imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $img["width"] , $img["height"] );             

      if ('jpg' == $type || 'jpeg' == $type)
                imageinterlace($thumbImg, $interlace);      

      $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
      $thumbName= $img['path'].".".$this->ThumbWidth.".".$type;
      $imageFun($thumbImg, $thumbName);
      imagedestroy($thumbImg);
      imagedestroy($srcImg);
      
      $img['path'] =$thumbName;
      $img['width'] =$width;
      $img['height'] =$height;

      return $img;
  }

  
  /**
   * 根据目录进行图片处理
   * @return [type] [description]
   */
  private function searchImages() {
    $DirDoor = new DirectoryIterator($this->path_dir);
    $MaxWidth=600;
    foreach($DirDoor as $dir) {
      if ($dir -> isFile()) {
        $Path = $dir -> getPathName();
        // list($width, $height, $type, $attr,$bits,$channels,$mime) = getimagesize($Path);
        if (($imgattr = getimagesize($Path)) != false) {
          $imgattr = array_values($imgattr);
          list($w, $h, $t, $a, $b, $c, $m) = $imgattr;
          $this->F_Array[] = array('path' => $Path, 
            "type" 		=> $this -> ImagesTypes[$t],
            "width" 	=> $w, 
            "height" 	=> $h,
            "mime" 		=> $m
            );
          $MaxWidth = ($MaxWidth>=$w) ? $w : $MaxWidth ;
        }
      }
    }

    $this->ThumbWidth = ($MaxWidth>=$this->ThumbWidth) ? $this->ThumbWidth : $MaxWidth ;
    


    /**
     * 再次循环进行图片缩略
     * @var [type]
     */
    foreach ($this->F_Array as $key => $img) {
      if($img['width']>$this->ThumbWidth){
          $img = $this->thumbImage($img);
          $this->F_Array[$key] =$img;
      }
      $this->ThumbHeight +=($img['height']+0);
    }

  }





  function temps() {
    $this->searchImages();
    $this->GenerateImg();
  }


}